package com.shop.shoporder.OrderTimingTask;

import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.common.UserRequest;
import com.shop.shoporder.entity.order.Order;
import com.shop.shoporder.service.CartService;
import com.shop.shoporder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class AutoReceivingGoods {
    @Autowired
    OrderService orderService;
    @Autowired
    CartService cartService;

    @Scheduled(cron = "0 0 02 * * ?")//每天凌晨2点
    public void AutoReceivingGoods() {
        String ip = UserRequest.getServerIp();
        if (!ip.equals("192.168")) {
            //自动收货
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = new Date();
            String date = sdf.format(date1);
            //Date dateX = InterfaceVer.addDateMinut(date, -240);//往前推10天

            Order order1 = new Order();
            order1.setOrderStatus("703");
            //order1.setDeliveryTime(dateX);

            List<String> listOrderNo = new ArrayList<>();
            List<Order> list = orderService.selectByOrderStatus(order1);
            //System.out.println("list:"+list.size());
            if (list != null && list.size() > 0) {
                for (Order order : list) {
                    String orderNo = order.getOrderNo() == null ? "" : order.getOrderNo();
                    Date deliveryTime = order.getDeliveryTime();
                    Date datedeliveryTime = InterfaceVer.addDateMinut(sdf.format(deliveryTime), +168);//往后推7天
                    if (!orderNo.equals("")) {
                        if (datedeliveryTime.getTime() > date1.getTime()) {
                            listOrderNo.add(orderNo);
                        }
                    }
                }
                int res = orderService.upAutoReceivingGoods(new Date(), listOrderNo, "705");
            }
        }
    }

    @Scheduled(cron = "0 0 02 * * ?")//每天凌晨2点
    public void CancalOrder() {
        String ip = UserRequest.getServerIp();
        if (!ip.equals("192.168")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = new Date();
            String date = sdf.format(date1);
            Date dateX = InterfaceVer.addDateMinut(date, -24);//往前推1天

            Order order1 = new Order();
            order1.setOrderStatus("701");
            order1.setpOrderTime(dateX);
            //order1.setDeliveryTime(dateX);

            List<String> listOrderNo = new ArrayList<>();
            List<Order> list = orderService.selectByOrderStatus1(order1);
            if (list != null && list.size() > 0) {
                for (Order order : list) {
                    String orderNo = order.getOrderNo() == null ? "" : order.getOrderNo();
                    if (!orderNo.equals("")) {
                        listOrderNo.add(orderNo);
                    }
                }
                int res = orderService.upAutoCancalOrder(new Date(), listOrderNo, "706");
            }
        }
    }

}
